package tech.yangzhijie.basic.knowledge.other;

public class VolatileTest {

    public static void main(String[] args) {
        VolatileDemo volatileDemo = new VolatileDemo();
        new Thread(volatileDemo).start();

        while(true){
            if(volatileDemo.isFlag()){
                System.out.println("--------------------------");
                break;
            }
        }
    }
}

class VolatileDemo implements Runnable{
    private volatile boolean flag = false;


    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setFlag(true);
        System.out.println("flag = " + flag);
    }

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
